﻿Imports IntoNails.DataAccess
Imports IntoNails.Models
Imports IntoNails.My.Resources

Namespace ViewModel
	Public Class AdminRegistrationViewModel
        Inherits SubWorkspaceViewModel
		
		#Region "Fields"
		
		'= User.CreateNewUser()
		Private ReadOnly _users As UserRepository
		Private ReadOnly _employees As EmployeeRepository
		Private ReadOnly _authorities As AuthorityRepository
		#End Region
		
		#Region "Constructors"
		
        Public Sub New()
            Me.DisplayName = StringResources.INTONAILS_ADMIN_REGISTRATION_VIEWMODEL_DISPLAYNAME

            _users = New UserRepository(String.Format(StringResources.USER_FILE_PATH, Environment.CurrentDirectory))
            _authorities = New AuthorityRepository(String.Format(StringResources.AUTHORITY_FILE_PATH, Environment.CurrentDirectory))
            _employees = New EmployeeRepository(String.Format(StringResources.EMPLOYEE_FILE_PATH, Environment.CurrentDirectory))
            'MyBase.New(user, repository)
            'MyBase._bindingEmployee = Me.DummyUser

        End Sub
		
		#End Region
		
		#Region "Delegate and Event"
		
		Public Event RegistrationCompleted As AdminRegistrationCallback
		
		Protected Overridable Sub OnAdminRegistrationCompleted(ByVal e As AdminRegistrationEventArgs)
			Dim handler As AdminRegistrationCallback = Me.RegistrationCompletedEvent
			If handler IsNot Nothing Then
				handler.Invoke(Me, e)
			End If
		End Sub
		
		' TODO: define an cancellable event, not this simble EventHandler
		Public Event RegistrationCancelled As EventHandler
		
		Protected Overridable Sub OnAdminRegistrationCancelled(ByVal e As EventArgs)
			Dim exitHandler As EventHandler = Me.RegistrationCancelledEvent
			If exitHandler IsNot Nothing Then
				exitHandler.Invoke(Me, e)
			End If
		End Sub
		
		#End Region
		
		#Region "Properties"

        Private _username As String
        Public Property Username() As String
            Get
                Return _username
            End Get
            Set(ByVal value As String)
                If value = _username Then
                    Return
                End If

                _username = value
                Me.OnPropertyChanged("Username")
            End Set
        End Property

        Private _password As String
        Public Property Password() As String
            Get
                Return _password
            End Get
            Set(ByVal value As String)
                If value = _password Then
                    Return
                End If

                _password = value
                Me.OnPropertyChanged("Password")
            End Set
        End Property

        Private _retypePwd As String
        Public Property ReTypePassword() As String
            Get
                Return _retypePwd
            End Get
            Set(ByVal value As String)
                _retypePwd = value
            End Set
        End Property

        Private _fullName As String
        Public Property FullName() As String
            Get
                Return _fullName
            End Get
            Set(ByVal value As String)
                _fullName = value
            End Set
        End Property

        Private _birth As Date
        Public Property BirthDate() As Date
            Get
                Return _birth
            End Get
            Set(ByVal value As Date)
                _birth = value
            End Set
        End Property

        Private _address As String
        Public Property Address() As String
            Get
                Return _address
            End Get
            Set(ByVal value As String)
                _address = value
            End Set
        End Property

        Private _contactNum As String
        Public Property ContactNumber() As String
            Get
                Return _contactNum
            End Get
            Set(ByVal value As String)
                _contactNum = value
            End Set
        End Property

        Private _cellPhone As String
        Public Property CellPhone() As String
            Get
                Return _cellPhone
            End Get
            Set(ByVal value As String)
                _cellPhone = value
            End Set
        End Property

#End Region
		
		#Region "Commands"
		
		Private _registerCommand As ICommand
		Public ReadOnly Property RegisterCommand() As ICommand
			Get
				If _registerCommand Is Nothing Then
					_registerCommand = New SimpleCommand(AddressOf Me.Register, AddressOf Me.CanRegister)
				End If
				Return _registerCommand
			End Get
		End Property
		
		Private _exitCommand As ICommand
		Public ReadOnly Property ExitCommand() As ICommand
			Get
				If _exitCommand Is Nothing Then
					_exitCommand = New SimpleCommand(AddressOf Me.ExecuteExit)
				End If
				Return _exitCommand
			End Get
		End Property
		
		
		#End Region
		
		#Region "Function"
		
		''' <summary>
		''' Registers admin profile, and fires the registration's event to the main view-model.
		''' </summary>
		Private Sub Register()
            Dim admin As User = User.CreateUser(Me.Username, Me.Password.Hash(), Date.Today, 1, 1)

            _users.Add(admin)
            _users.Save()

            ' process the AuthorityRepository first...
            Dim adminAuthority As Authority = Authority.CreateAuthority(1, "Admin", Date.Today, True)
            _authorities.Add(adminAuthority)
            _authorities.Save()

            ' fires the registration's event to the main view-model.
            Me.OnAdminRegistrationCompleted(New AdminRegistrationEventArgs(admin))

            ' creates an employee's profile which related to admin...
            Dim e As Employee = Employee.CreateEmployee(admin.EmployeeCode, Me.FullName, Me.BirthDate, Me.Address, Me.ContactNumber, Me.CellPhone)
            _employees.Add(e)
            _employees.Save()
        End Sub

        ''' <summary>
        ''' Checks if we can fire the RegisterCommand
        ''' </summary>
        <DebuggerStepThrough()> _
        Private Function CanRegister() As Boolean
            ' TODO: we can use IDateErrorInfo here...
            Return Me.Username <> String.Empty And _
             Me.Password <> String.Empty And _
             Me.Password = Me.ReTypePassword

            ' we can update the employee profile related to this user later...
        End Function

        Private Sub ExecuteExit()
            Me.OnAdminRegistrationCancelled(EventArgs.Empty)
        End Sub

#End Region

    End Class
End Namespace